import requests

'''
具体步骤
1. 
    url:https://passport.iqiyi.com/apis/reglogin/login.action 
    method:post

2.  https://passport.iqiyi.com/apis/user/sso/cd/token.action
    method:get

3
    url:https://passport.iqiyi.com/apis/user/info.action
    method:post 
'''

token_param = {
    "agenttype": "1",
    "callback": "window.lib.__callbacks__.cbfcxi73",
    "ptid": "01010021010000000000",
    "qd_sc": "5a94413244063ded305c8b989cec1123"
}

info_form_data = {
    "agenttype": "1",
    "ptid": "01010021010000000000",
    "lang": "",
    "fromSDK": "1",
    "sdk_version": "1.0.0",
    "authcookie": "faRZZSBv96nNpYEeiLL0ntvkXXOtHu20L6hym3hpXZTG8PfyGZPooCnecvoYoLm36OKw3b",
    "antiCsrf": "597119cc8c31bb00f4d967e653010a82",
    "fields": "insecure_account,userinfo"
}


class AiQiYi:

    def __init__(self, email: str, passwd: str):
        self.email = email
        self.passwd = passwd
        self.session = requests.Session()

    def _login(self):
        assert self.email is not None, "用户名不能为空"
        assert self.passwd is not None, "密码不能为空"
        api = "https://passport.iqiyi.com/apis/reglogin/login.action"
        login_form_data = {
            "email": self.email,
            "fromSDK": "1",
            "sdk_version": "1.0.1",
            "passwd": self.passwd,
            "agenttype": "1",
            "__NEW": "1",
            "checkExist": "1",
            "lang": "",
            "ptid": "01010021010000000000",
            "nr": "1",
            "verifyPhone": "1",
            "area_code": "86",
            "dfp": "a0ec30917570074dffb0cde2a4ccb84abe2e5ca1734d37356a9cedefe25e6d709b",
            "envinfo": "eyJqbiI6Ik1vemlsbGEvNS4wIChXaW5kb3dzIE5UIDEwLjA7IFdpbjY0OyB4NjQpIEFwcGxlV2ViS2l0LzUzNy4zNiAoS0hUTUwsIGxpa2UgR2Vja28pIENocm9tZS83Ni4wLjM4MDkuMTAwIFNhZmFyaS81MzcuMzYiLCJjbSI6InpoLUNOIiwiZ3UiOjI0LCJ1ZiI6MSwianIiOlsxOTIwLDEwODBdLCJkaSI6WzE5MjAsMTA0MF0sInpwIjotNDgwLCJ1aCI6MSwic2giOjEsImhlIjoxLCJ6byI6MSwicnYiOiJ1bmtub3duIiwibngiOiJXaW4zMiIsIml3IjoidW5rbm93biIsInFtIjpbIkNocm9tZSBQREYgUGx1Z2luOjpQb3J0YWJsZSBEb2N1bWVudCBGb3JtYXQ6OmFwcGxpY2F0aW9uL3gtZ29vZ2xlLWNocm9tZS1wZGZ+cGRmIiwiQ2hyb21lIFBERiBWaWV3ZXI6Ojo6YXBwbGljYXRpb24vcGRmfnBkZiIsIk5hdGl2ZSBDbGllbnQ6Ojo6YXBwbGljYXRpb24veC1uYWNsfixhcHBsaWNhdGlvbi94LXBuYWNsfiJdLCJ3ciI6ImI3NzY2NGM3MTcwNzdhZmZmMzNhN2QyODM2ZTIzNzdjIiwid2ciOiIwNGRlNmRiOThlMWY4NjFlZGFiN2VjMWJkZmI4MDBiZiIsImZrIjpmYWxzZSwicmciOmZhbHNlLCJ4eSI6ZmFsc2UsImptIjpmYWxzZSwiYmEiOmZhbHNlLCJ0bSI6WzAsZmFsc2UsZmFsc2VdLCJhdSI6dHJ1ZSwibWkiOiIzMjhlMTliNC1jNGFjLWEzNDMtMDU3Mi1lMTE2ZWUyMjIwY2MiLCJjbCI6IlBDV0VCIiwic3YiOiIxLjAiLCJqZyI6IjZlODgwODlmNWEwYzhjYjA2ODJlNTc1ODA0YjYzNTIyIiwiZmgiOiJ0d3A4M21ldTBmcG9odDhub2RreGplOSIsImlmbSI6W3RydWUsNDYwLDQyMCwiaHR0cDovL3d3dy5pcWl5aS5jb20vIl0sImV4IjoiIiwiZHYiOiJvZmYiLCJwdiI6ZmFsc2V9"
        }
        headers = {
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Referer": "http://www.iqiyi.com/iframe/loginreg?ver=1",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Sec-Fetch-Mode": "cors",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
        }

        try:
            response = self.session.post(url=api, headers=headers, data=login_form_data)
            response.raise_for_status()
        except Exception:
            return

        print(response.cookies)
        print(response.json())
        response = self.session.get(url="https://pcw-api.iqiyi.com/passport/user/userinfodetail?version=1",
                         headers={
                             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
                             "Referer": "https://www.iqiyi.com/"
                         })
        print(response.json())
    def test(self):
        self._login()


if __name__ == '__main__':
    email = "187167"
    passwd = "728482e84c9e3e73cb14ea80aec2008469c5c6df9a8e96d93355006cbc0417c1433da2fefda131e9dd414069b27198da249dda52f7944b6fa99a595ce0e5a716"
    aqy = AiQiYi(email, passwd)
    aqy.test()
    "喜欢吃麻辣香锅的纳兰兴言"
